import Common from "../Common";

const {ccclass, property} = cc._decorator;

@ccclass
export default class UserLevelItem extends Common {

    @property(cc.Label)
    titleLabel: cc.Label = null;

    @property(cc.Label)
    numberLabel: cc.Label = null;

    @property(cc.Node)
    statusBtn: cc.Node = null;

    @property(cc.Label)
    statusBtnLabel: cc.Label = null;

    @property(cc.Node)
    treeNode: cc.Node = null;

    // 0:未升级 1已升级 2去升级 3去激活
    status: number = 0

    isEmpty: boolean = true

    protected start() {
        this.statusBtn.on(cc.Node.EventType.TOUCH_START, () => {
            if (this.status >= 2) {
                this.onBtnClick()
                cc.director.loadScene('shop')
            }
        })
    }

    setStatus(status: number) {
        this.status = status
        switch (status) {
            case 0:
                this.setStatusBtnText('未升级')
                this.setBtnBg('btn_bg_1', '#999999')
                break;
            case 1:
                this.setStatusBtnText('已升级')
                this.setBtnBg('btn_bg_2', '#ff4400')
                break;
            case 2:
                this.setStatusBtnText('去升级')
                this.setBtnBg('btn_bg', '#262334')
                break;
            case 3:
                this.setStatusBtnText('去激活')
                this.setBtnBg('btn_bg', '#262334')
                break;

        }
    }

    setTitle(title: string) {
        this.titleLabel.string = title
    }

    setNumber(number: number | string) {
        this.numberLabel.string = `${number}人`
    }

    setIsEmpty(val = true) {
        this.isEmpty = val;
        this.node.children.forEach(item => {
            item.active = !val;
        })
    }

    async setLevel(level: number) {
        const treeAtlas = await this.getTreeAtlas();
        this.treeNode.getComponent(cc.Sprite).spriteFrame = treeAtlas.getSpriteFrame(`${level}`)
    }

    protected setBtnBg(spriteFrameName: string, color: string) {
        cc.resources.load(`btn/${spriteFrameName}`, cc.SpriteFrame, (err, item: cc.SpriteFrame) => {
            if (err) {
                return;
            }
            this.statusBtn.getComponent(cc.Sprite).spriteFrame = item;
            this.statusBtnLabel.node.color = cc.Color.fromHEX(cc.color(), color)
        })
    }

    protected setStatusBtnText(text: string) {
        this.statusBtnLabel.string = text
    }

}
